package vip.ablog.xmall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import vip.ablog.xmall.product.entity.AttrEntity;
import vip.ablog.xmall.product.entity.AttrGroupEntity;
import vip.ablog.xmall.product.service.AttrGroupService;
import vip.ablog.common.utils.PageUtils;
import vip.ablog.common.utils.R;
import vip.ablog.xmall.product.service.AttrService;
import vip.ablog.xmall.product.service.CategoryService;
import vip.ablog.xmall.product.vo.AttrGroupRelationVo;
import vip.ablog.xmall.product.vo.AttrGroupWithAttrsVo;

import javax.annotation.Resource;


/**
 * 属性分组
 *
 * @author jsp
 * @email jsp@gmail.com
 * @date 2020-08-10 17:11:08
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Resource
    private AttrGroupService attrGroupService;
    @Resource
    private CategoryService categoryService;
    @Resource
    AttrService attrService;


    @PostMapping("/attr/relation")
    public R relation(){

        return R.ok();
    }

    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupsWithAttrs(@PathVariable("catelogId")Long catelogId){

        //查出当前分类下的所有属性和分组
        //查出每个属性分组的所有属性

        List<AttrGroupWithAttrsVo> attrsVos = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);

        return R.ok().put("data",attrsVos);

    }

    @GetMapping("{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> attrEntityList = attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data",attrEntityList);
    }

    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String ,Object> params){
        PageUtils page = attrService.getNoAttrRelation(params, attrgroupId);

        return R.ok().put("data", page);
    }

    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] relationVos){
        attrService.deleteRelation(relationVos);
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    ////@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Long catelogId){

        //PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params, catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    ////@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long[] path = categoryService.findCatelogPath(attrGroup.getCatelogId());
        attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    ////@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
   // //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
  //  //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
